package com.alipear.uibase;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.lang.ref.WeakReference;
import java.util.HashMap;

/**
 * Created by zoulinlin on 2015/4/2.
 */
public class ViewFinder {
    public WeakReference<View> getContainer() {
        return mContainer;
    }

    WeakReference<View> mContainer;
    HashMap<Integer, View> mViewMaps;
    WeakReference<ImageLoadWrapper> mLoaderWrapper;

    public int getItemIndex() {
        return itemIndex;
    }

    public void setItemIndex(int itemIndex) {
        this.itemIndex = itemIndex;
    }

    private int itemIndex;

    public ViewFinder(View container) {
        mViewMaps = new HashMap<Integer, View>();
        mContainer = new WeakReference<View>(container);
    }

    public ViewFinder(View container, ImageLoadWrapper wrapper) {
        mViewMaps = new HashMap<Integer, View>();
        mContainer = new WeakReference<View>(container);
        mLoaderWrapper = new WeakReference<ImageLoadWrapper>(wrapper);
    }

    public View findView(int id) {
        View tmp = mViewMaps.get(id);
        if (tmp == null) {
            tmp = mContainer.get().findViewById(id);
        }
        if (tmp != null) {
            return tmp;
        }
        return null;
    }

    public TextView findTextView(int id) {
        return (TextView)findView(id);
    }
    public Button findButton(int id) {
        return (Button)findView(id);
    }
    public ImageButton findImageButton(int id) {
        return (ImageButton)findView(id);
    }
    public ImageView findImageView(int id) {
        return (ImageView)findView(id);
    }
    public LinearLayout findLinearLayout(int id) {
        return (LinearLayout)findView(id);
    }
    public RelativeLayout findRelativeLayout(int id) {
        return (RelativeLayout)findView(id);
    }
    public FrameLayout findFrameLayout(int id) {
        return (FrameLayout)findView(id);
    }
    public RadioButton findRadioButton(int id) {
        return (RadioButton)findView(id);
    }
    public RadioGroup findRadioGroup(int id) {
        return (RadioGroup)findView(id);
    }
    public CheckBox findCheckBox(int id) {
        return (CheckBox)findView(id);
    }
    public ListView findListView(int id) {
        return (ListView)findView(id);
    }

    public void bindImage(int id, String url, int defaultRes) {
        ImageView logoView = findImageView(id);
        if (url != null) {
            if (logoView == null) {
                Log.e("iconWithName", "logo view is null");
            }
            Object tag = logoView.getTag();
            if (tag != null && tag instanceof String && ((String)tag).compareTo(url) == 0) {
                mLoaderWrapper.get().bindImage(logoView, url, logoView.getDrawable());
            }
            else {
                mLoaderWrapper.get().bindImage(
                        logoView,
                        url,
                        logoView.getResources().getDrawable(defaultRes));
                if (tag == null ||tag instanceof String) {
                    logoView.setTag(url);
                }
            }
        }
        else {
            logoView.setImageDrawable(logoView.getResources().getDrawable(defaultRes));
        }
    }

}
